//
//  WHSearchDetailResultController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/14.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHSearchDetailResultController.h"
#import "WHNormalDesCell.h"
#import "WHExpressInfoModel.h"

@interface WHSearchDetailResultController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTableView;
@property (nonatomic, strong) NSMutableArray *tipsArray;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation WHSearchDetailResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"查询结果";
    [self initParams];
    [self setUpTableView];
    [self selectMessager];
}

- (void)initParams {
    _tipsArray = @[].mutableCopy;
    _dataArray = @[].mutableCopy;
}

- (void)setUpTableView {
    UITableView *dataTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    dataTableView.delegate = self;
    dataTableView.dataSource = self;
    [self.view addSubview:dataTableView];
    
    [dataTableView registerClass:[WHNormalDesCell class] forCellReuseIdentifier:NSStringFromClass([WHNormalDesCell class])];
    
    dataTableView.tableFooterView = [UIView new];
    dataTableView.backgroundColor = KClearColor;
    dataTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.equalTo(@kTopHeight);
    }];
    
    _dataTableView = dataTableView;
}

#pragma mark - —————— UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tipsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WHNormalDesCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHNormalDesCell class]) forIndexPath:indexPath];
    cell.tipsLabel.text = _tipsArray[indexPath.row];
    cell.desLabel.text = _dataArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealwidth(35);
}

#pragma mark - —————— 接口 ——————
- (void)selectMessager {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_selectMessager);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @(userManager.detailUserInfo.userId), @"userId",
                          self.waybillNum, @"waybillNum",
                          self.expressCompany, @"expressCompany",
                          nil];
    
    [WHNetworkManager requestWithURL:postURL parameters:dict isUpData:YES Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                NSDictionary *retDict = responseObject[@"retobj"];
                
                WHExpressInfoModel *model = [WHExpressInfoModel modelWithDictionary:retDict];
                
                [self configTableViewBy:model];
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)configTableViewBy:(WHExpressInfoModel *)model {
    NSString *statusStr = @""; // （0：入库失败；1：入库成功；2：未入库；3:出库失败；4：出库成功）
    switch (model.intoStatus) {
        case 0:
            statusStr = @"入库失败";
            break;
        case 1:
            statusStr = @"已入库";
            break;
        case 2:
            statusStr = @"未入库";
            break;
        case 3:
            statusStr = @"出库失败";
            break;
        case 4:
            statusStr = @"已取件";
            break;
            
        default:
            break;
    }
    
    if (model.intoStatus == 2) {
        [_tipsArray addObjectsFromArray:@[@"快递类型", @"运单号", @"状态"]];
        [_dataArray addObjectsFromArray:@[model.expressType, model.waybillNum, statusStr]];
        
    } else {
        [_tipsArray addObjectsFromArray:@[@"快递类型", @"运单号", @"快递公司", @"快递员", @"状态", @"取件码", @"收件人手机"]];
        [_dataArray addObjectsFromArray:@[model.expressType, model.waybillNum, model.expressCompany, model.expressPerson, statusStr, model.takeCode, model.mobile]];
        
    }
    
    [self.dataTableView reloadData];
}


@end
